import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../im/page/bloc/DraggableBloc.dart';
import '../../im/page/view/DraggableFloatingView.dart';
import '../../im/util/DialogUtil.dart';
import 'EasyRefreshCubit.dart';

class EasyRefreshPage extends StatefulWidget {
  const EasyRefreshPage({super.key});

  @override
  State<EasyRefreshPage> createState() => _EasyRefreshPageState();
}

class _EasyRefreshPageState extends State<EasyRefreshPage> {

  EasyRefreshController _refreshController = EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true);

  @override
  void initState() {
    super.initState();

    var easyRefreshCubit = context.read<EasyRefreshCubit>();
    easyRefreshCubit.add(EasyRefreshInitEvent());
  }

  @override
  Widget build(BuildContext context) {
    var easyRefreshCubit = context.watch<EasyRefreshCubit>();
    return Scaffold(
      body: Column(children: [
        Expanded(child: EasyRefresh(
          controller: _refreshController,
          onRefresh: (){
            easyRefreshCubit.add(EasyRefreshLoadOldsEvent(_refreshController));
          },
          onLoad: (){
            easyRefreshCubit.add(EasyRefreshLoadNewsEvent(_refreshController));
          },
          child: ListView.builder(
            itemBuilder: (c, i) => Card(child: Center(child: Text(easyRefreshCubit.state.dataList[i]))),
            itemCount: easyRefreshCubit.state.dataList.length,
          ),
        )),
        TextButton(onPressed: ()=>overlay(), child: Text('ssss')),
      ],),
    );
  }

  void overlay() {
    var child = BlocProvider(create: (_)=> DraggableCubit(), child: DraggableFloatingCubitView(child: Material(
      elevation: 4.0,
      borderRadius: BorderRadius.all(Radius.circular(10)),
      child: Container(
        width: 200,
        height: 100,
        padding: EdgeInsets.all(16.0),
        color: Colors.white,
        child: Text('11111111111111111111'),
      ),
    ),),);
    showOverlay(context, child);
  }
}